home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Online / NNTPd / server / parsit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-01  |  2.7 KB  |  113 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)parsit.c    1.4    (Berkeley) 8/27/89";
  3. #endif
  4.  
  5. /*
  6.  * Parse a string of words separated by spaces into an
  7.  * array of pointers to characters, just like good ol' argv[]
  8.  * and argc.
  9.  *
  10.  * Usage:
  11.  *
  12.  * char line[132];
  13.  * char **argv;
  14.  * int argc;
  15.  *
  16.  *    argv = (char **) NULL;
  17.  *    argc = parsit(line, &argv);
  18.  *
  19.  * returns the number of words parsed in argc.  argv[argc] will
  20.  * be (char *) NULL to indicate end of list, if you're not
  21.  * happy with just knowing how many words you have.
  22.  *
  23.  * Note that setting argv = (char **) NULL is only done the first
  24.  * time the routine is called with a new "argv" -- it tells
  25.  * parsit that "argv" is a new array, and parsit shouldn't free
  26.  * up the elements (as it would do if it were an old array).
  27.  *
  28.  *    Phil Lapsley
  29.  *    College of Engineering
  30.  *    University of California, Berkeley
  31.  *    (ARPA: phil@Berkeley.ARPA; UUCP: ...!ucbvax!phil)
  32.  */
  33.  
  34. #include <stdio.h>
  35.  
  36. extern    char    *malloc(), *strcpy();
  37.  
  38. int
  39. parsit(line, array)
  40. char *line;
  41. char ***array;
  42. {
  43.     char    **argv;
  44.     char    *word;
  45.     char    *linecp;
  46.     int    i, j, num_words,longest_word;
  47.  
  48.     argv = *array;
  49.     if (argv != (char **) NULL) {  /* Check to see if we should */
  50.         for (i = 0; argv[i] != (char *) NULL; i++)    /* free */
  51.             free(argv[i]);    /* the old array */
  52.         free((char *) argv);    /* and then free the ptr itself */
  53.     }
  54.  
  55.     linecp = line;
  56.     num_words = longest_word = 0;
  57.     while (1) {    /* count words in input */
  58.         for (; *linecp == ' ' || *linecp == '\t'; ++linecp)
  59.             ;
  60.         if (*linecp == '\0')
  61.             break;
  62.         word = linecp;
  63.         for (; *linecp != ' ' && *linecp != '\t' && *linecp != '\0'; ++linecp)
  64.             ;
  65.         ++num_words;
  66.         if ((i = linecp - word) > longest_word) longest_word = i;
  67.         if (*linecp == '\0')
  68.             break;
  69.     }
  70.  
  71.     /* Then malloc enough for that many words plus 1 (for null) */
  72.  
  73.     if ((argv = (char **) malloc((num_words + 1) * sizeof(char *))) ==
  74.         (char **) NULL) {
  75.         fprintf(stderr, "parsit: malloc out of space!\n");
  76.         return(0);
  77.     }
  78.     /* malloc enough the longest word */
  79.  
  80.     if ((word = (char *) malloc(longest_word+1)) ==    (char *) NULL) {
  81.         fprintf(stderr, "parsit: malloc out of space!\n");
  82.         return(0);
  83.     }
  84.  
  85.     j = i = 0;
  86.     while (1) {    /* Now build the list of words */
  87.         for (; *line == ' ' || *line == '\t'; ++line)
  88.             ;
  89.         if (*line == '\0')
  90.             break;
  91.  
  92.         i = 0;
  93.         for (; *line != ' ' && *line != '\t' && *line != '\0'; ++line)
  94.             word[i++] =  *line;
  95.         word[i] = '\0';
  96.         argv[j] = malloc(strlen(word) + 1);
  97.         if (argv[j] == (char *) NULL) {
  98.             fprintf(stderr, "parsit: malloc out of space!\n");
  99.             free(word);
  100.             return(0);
  101.         }
  102.  
  103.         (void) strcpy(argv[j], word);
  104.         ++j;
  105.         if (*line == '\0')
  106.             break;
  107.     }
  108.     argv[j] = (char *) NULL;  /* remember null at end of list */
  109.     *array = argv;
  110.     free(word);
  111.     return(j);
  112. }
  113.